package com.fengwk.support.util;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.MalformedURLException;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

import com.fengwk.support.exception.BaseException;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Image;
import com.itextpdf.text.PageSize;
import com.itextpdf.text.Rectangle;
import com.itextpdf.text.pdf.PdfWriter;

public class PdfUtils {

	private static final Rectangle DEFAULT_PAGE_SIZE = PageSize.A4;
	private static final float DEFAULT_MARGIN_LEFT = 30;
	private static final float DEFAULT_MARGIN_RIGHT = 30;
	private static final float DEFAULT_MARGIN_TOP = 30;
	private static final float DEFAULT_MARGIN_BOTTOM = 30;
	
	private PdfUtils() {
		throw new BaseException("can not instance " + getClass());
	}
	
	public void mergeImageToPdf(File[] files, String outPath) throws MalformedURLException, IOException, DocumentException {
		mergeImageToPdf(
				files, 
				outPath, 
				DEFAULT_PAGE_SIZE, 
				DEFAULT_MARGIN_LEFT, 
				DEFAULT_MARGIN_RIGHT, 
				DEFAULT_MARGIN_TOP, 
				DEFAULT_MARGIN_BOTTOM);
	}
	
	public void mergeImageToPdf(List<File> fileList, String outPath) throws MalformedURLException, IOException, DocumentException {
		mergeImageToPdf(
				fileList, 
				outPath, 
				DEFAULT_PAGE_SIZE, 
				DEFAULT_MARGIN_LEFT, 
				DEFAULT_MARGIN_RIGHT, 
				DEFAULT_MARGIN_TOP, 
				DEFAULT_MARGIN_BOTTOM);
	}
	
	public void mergeImageToPdf(
			File[] files, 
			String outPath, 
			Rectangle pageSize, 
			float marginLeft, 
			float marginRight,
			float marginTop, 
			float marginBottom) throws MalformedURLException, IOException, DocumentException {
		List<File> fileList = Arrays.asList(files);
		mergeImageToPdf(fileList, outPath, pageSize, marginLeft, marginRight, marginTop, marginBottom);
	}
	
	public void mergeImageToPdf(
			List<File> fileList, 
			String outPath, 
			Rectangle pageSize, 
			float marginLeft, 
			float marginRight,
			float marginTop, 
			float marginBottom) throws MalformedURLException, IOException, DocumentException {
		
		Document document = new Document(pageSize, marginLeft, marginRight, marginTop, marginBottom);
		PdfWriter.getInstance(document, new FileOutputStream(outPath + ".pdf"));
		document.open();

		float pageW = pageSize.getWidth() - marginLeft - marginRight;
		float pageH = pageSize.getHeight() - marginTop - marginBottom;

		float percent = 100;
		float w = 0, h = 0;
		float wBuf = 0, hBuf = 0;
		
		Collections.sort(fileList, (f1, f2) -> {
			String name1 = FileExtensionUtils.split(f1.getName())[0];
			String name2 = FileExtensionUtils.split(f2.getName())[0];
			if (RegexUtils.match(name1, "^\\d+$") && RegexUtils.match(name2, "^\\d+$")) {
				return Long.valueOf(name1).compareTo(Long.valueOf(name2));
			} else {
				return name1.compareTo(name2);
			}
		});
		
		for (int i = 0; i < fileList.size(); i++) {
			Image img = Image.getInstance(fileList.get(i).getCanonicalPath());
			// 图片缩放
			w = img.getWidth();
			h = img.getHeight();
			if (w > pageW) {
				hBuf = w / pageW * pageH;
				wBuf = pageW;
			}
			if (h > pageH) {
				wBuf = h / pageH * pageW;
				hBuf = pageH;
			}
			if (hBuf < h) {
				percent = hBuf / h;
			} else if (wBuf < w) {
				percent = wBuf / w;
			}
			percent = percent * 100;
			img.scalePercent(percent);
			document.add(img);
		}
		document.close();
	}
	
}
